<template>
  <Card style="min-height: 600px" class="search">
    <Card  style="width: 90%;margin: 0 auto;background-color: ghostwhite;font-weight: bolder;font-size: 12px;font-family: '微软雅黑 Light'" class="drawer-content">
      <p slot="title" >
        会员列表
      </p>
    <Row v-show="openSearch" @keydown.enter.native="handleSearch">

        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
          <FormItem label="卡号" prop="memberCardNo">
            <Input type="text" v-model="searchForm.memberCardNo" placeholder="请输入卡号" clearable style="width: 200px"/>
          </FormItem>
          <FormItem label="姓名" prop="memberName">
            <Input type="text" v-model="searchForm.memberName" placeholder="请输入姓名" clearable style="width: 200px"/>
          </FormItem>
          <FormItem label="手机号" prop="memberTel">
            <Input type="text" v-model="searchForm.memberTel" placeholder="请输入手机号" clearable style="width: 200px"/>
          </FormItem>
          <FormItem style="margin-left:-35px;" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Form>
      </Row>
      <Row class="operation">
        <Button @click="add" type="primary" icon="md-add">添加</Button>
        <Button @click="delAll" icon="md-trash">批量删除</Button>
        <Button @click="getDataList" icon="md-refresh">刷新列表</Button>
      </Row>

      <Table
          :loading="loading"
          border
          :columns="columns"
          :data="data"
          ref="table"
          sortable="custom"
          @on-sort-change="changeSort"
          @on-selection-change="changeSelect"
      ></Table>
      <Row type="flex" justify="end" class="page">
        <Page
            :current="searchForm.pageNumber"
            :total="total"
            :page-size="searchForm.pageSize"
            @on-change="changePage"
            @on-page-size-change="changePageSize"
            :page-size-opts="[10,20,50]"
            size="small"
            show-total
            show-elevator
            show-sizer
        ></Page>
      </Row>

      <addEdit :data="form"   :type="showType" v-model="showDrawer" @on-submit="getDataList" />
    </Card>
  </Card>
</template>

<script>
// 根据你的实际请求api.js位置路径修改
import { getMemberList, deleteMember } from "@/api/index";
// 根据你的实际添加编辑组件位置路径修改
import addEdit from "./addEdit.vue";
import { shortcuts } from "@/libs/shortcuts";
export default {
  name: "member",
  components: {
    addEdit,
  },
  data() {
    return {
      openSearch: true, // 显示搜索
      openTip: true, // 显示提示
      showType: "0", // 添加或编辑标识
      showDrawer: false, // 显示添加编辑抽屉
      loading: true, // 表单加载状态
      searchForm: { // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        sort: "memberCardNo", // 默认排序字段
        order: "desc", // 默认排序方式
      },
      selectList: [], // 多选数据
      form: {},
      columns: [
        // 表头
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          title: "会员头像",
          width: 100,
          align: "center",
          render: (h, params) => {
            return h("Avatar", {
              props: {
                src: params.row.memberFaceUrl,
              },
            });
          },
        },
        {
          title: "卡号",
          key: "memberCardNo",
          minWidth: 120,
          sortable: false,
          render: (h, params) => {
            return h(
                "a",
                {
                  on: {
                    click: () => {
                      this.detail(params.row);
                    },
                  },
                },
                params.row.memberCardNo
            );
          },
        },
        {
          title: "姓名",
          key: "memberName",
          sortable: true,
          minWidth: 120,
          render: (h, params) => {
            return h(
                "a",
                {
                  on: {
                    click: () => {
                      this.detail(params.row);
                    },
                  },
                },
                params.row.memberName
            );
          },
        },
        {
          title: "手机号码",
          key: "memberTel",
          minWidth: 150,
          sortable: false,
        },
        {
          title: "会员生日",
          key: "memberBirthDay",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "会员性别",
          key: "memberSex",
          minWidth: 120,
          sortable: false,
        },

        {
          title: "会员等级",
          key: "memberLevel",
          minWidth: 100,
          sortable: false,
        },
        {
          title: "会员地址",
          key: "memberAddress",
          minWidth: 300,
          sortable: false,
        },
        {
          title: "开卡时间",
          key: "memberUseDate",
          minWidth: 120,
          fixed: "right",
          sortable: false,
        },
        {
          title: "过期时间",
          key: "memberEndDate",
          minWidth: 120,
          fixed: "right",
          sortable: false,
        },
        {
          title: "是否过期",
          key: "memberEndDate",
          minWidth: 200,
          fixed: "right",
          sortable: false,
          render:(h,params) =>{
            let re = "",
                color = "";
            if (params.row.memeberTimeStatus == 0 || params.row.memeberTimeStatus == -1) {
              //已过期
              color = "red";
            } else if (params.row.memeberTimeStatus == 1) {
              //即将过期
              color = "blue";
            } else {
              //还剩很久
              color = "green";
            }
            re = params.row.memeberTimeStatusStr;
            return h("div", [
              h(
                  "a",
                  {
                    props: {
                      color: color,
                    },
                    style:{
                      color:color,
                      fontWeight:'bolder'
                    }
                  },
                  re
              ),
            ]);

          }
        },
        {
          title: "操作",
          key: "action",
          fixed: "right",
          align: "center",
          width: 150,
          render: (h, params) => {
            //let  button = h("DropdownItem", { props: { name: "enable" } }, "启用");
            // if (params.row.status == 0) {
            //   button = h(
            //       "DropdownItem",
            //       { props: { name: "disable" } },
            //       "禁用"
            //   );
            // } else {
            //   button = h("DropdownItem", { props: { name: "enable" } }, "启用");
            // }
            return h("div", [

              h(
                  "Dropdown",
                  {
                    props: { transfer: true,trigger	:"click" },
                    on: {
                      "on-click": (v) => {
                        this.clickAction(params.row, v);
                      },
                    },
                  },
                  [
                    h("a", [
                      "更多操作",
                      h("Icon", {
                        props: {
                          type: "ios-arrow-down",
                        },
                      }),
                    ]),
                    h("DropdownMenu", { slot: "list" }, [
                      h(
                          "DropdownItem",
                          { props: { name: "edit" } },
                          "修改信息"
                      ),
                      h(
                          "DropdownItem",
                          { props: { name: "editPassWord" } },
                          "修改会员密码"
                      ),
                      h(
                          "DropdownItem",
                          { props: { name: "recharge" } },
                          "延长有效期"
                      ),
                      h(
                          "DropdownItem",
                          { props: { name: "recharge" } },
                          "签到情况"
                      ),
                      h(
                          "DropdownItem",
                          { props: { name: "recharge" } },
                          "预约情况"
                      ),
                      h(
                          "DropdownItem",
                          { props: { name: "syncDoorSystem" } },
                          "同步门禁系统"
                      ),
                      h("DropdownItem", { props: { name: "remove" } }, "删除"),
                    ]),
                  ]
              ),
            ]);
          },
        }
      ],
      data: [], // 表单数据
      pageNumber: 1, // 当前页数
      pageSize: 10, // 页面大小
      total: 0 // 表单数据总数
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    detail(v){
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = "";
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      this.form = data;
      this.showType = "0";
      this.showDrawer = true;
    },
    clickAction(row,v){
        if (v == 'edit'){
          this.edit(row)
        } else if (v == 'editPassWord'){
          //修改密码

        } else if (v == 'remove'){
          //删除
          this.remove(row)
        }
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order === "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    changeSelect(e) {
      this.selectList = e;
    },
    getDataList() {
      this.loading = true;
      getMemberList(this.searchForm).then(res => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.content;
          this.total = res.result.totalElements;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },
    add() {
      this.showType = "2";
      this.showDrawer = true;
    },
    edit(v) {
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = "";
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      this.form = data;
      this.showType = "1";
      this.showDrawer = true;
    },
    remove(v) {
      this.$Modal.confirm({
        title: "确认删除",
        // 记得确认修改此处
        content: "您确认要删除该条数据?",
        loading: true,
        onOk: () => {
          // 删除
          deleteMember({ids: v.id}).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.clearSelectAll();
              this.getDataList();
            }
          });
        }
      });
    },
    delAll() {
      if (this.selectList.length <= 0) {
        this.$Message.warning("您还未选择要删除的数据");
        return;
      }
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除所选的 " + this.selectList.length + " 条数据?",
        loading: true,
        onOk: () => {
          let ids = "";
          this.selectList.forEach(function(e) {
            ids += e.id + ",";
          });
          ids = ids.substring(0, ids.length - 1);
          // 批量删除
          deleteMember({ids: ids}).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.clearSelectAll();
              this.getDataList();
            }
          });
        }
      });
    }
  },
  mounted() {
    this.init();
  }
};
</script>
<style lang="less">
@import "@/styles/table-common.less";
</style>
